package com.ezandroid.library.network.http.request;

/**
 * Encapsulates a parsed response for delivery.
 *
 * @param <T> Parsed type of this response
 */
public class Response<T> {

    /**
     * Returns a successful response containing the parsed result.
     */
    public static <T> Response<T> success(T result) {
        return new Response<>(result);
    }

    /**
     * Returns a failed response containing the given error code and an optional
     * localized message displayed to the user.
     */
    public static <T> Response<T> error(RequestException error) {
        return new Response<>(error);
    }

    /**
     * Parsed response, or null in the case of error.
     */
    public final T result;

    /**
     * Detailed error information if <code>errorCode != OK</code>.
     */
    public final RequestException error;

    /**
     * True if this response was a soft-expired one and a second one MAY be coming.
     */
    public boolean intermediate = false;

    /**
     * Returns whether this response is considered successful.
     */
    public boolean isSuccess() {
        return error == null;
    }

    private Response(T result) {
        this.result = result;
        this.error = null;
    }

    private Response(RequestException error) {
        this.result = null;
        this.error = error;
    }
}